package com.ybxx.fees.uti;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.model.Residents;
import com.ybxx.base.model.Room;
import com.ybxx.base.model.RoomInfo;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.MoneyUtil;
import com.ybxx.fees.model.AnmeterRecordItem;
import com.ybxx.fees.model.AnmeterRecords;
import com.ybxx.fees.model.FfRecord;
import com.ybxx.fees.model.GlfRecord;
import com.ybxx.fees.model.HlfRecord;
import com.ybxx.fees.model.ItemVO;
import com.ybxx.fees.model.NqfRecord;
import com.ybxx.fees.model.OtherRecord;
import com.ybxx.fees.model.OtherRecordItem;
import com.ybxx.fees.model.Records;
import com.ybxx.fees.model.RecordsVO;
import com.ybxx.fees.model.WatermeterRecordItem;
import com.ybxx.fees.model.WatermeterRecords;
import com.ybxx.fees.model.WsfRecord;
import com.ybxx.fees.model.WsfRecordItem;
import com.ybxx.system.model.User;

public class ConvertRecord {
	public static RecordsVO convert(Records records,RoomInfo roomInfo,Room room,Residents residents,User user){
		RecordsVO vo=new RecordsVO();
		vo.setFrcode(records.getFrcode().substring(0,12));
		vo.setYmonth(records.getYmonth());
		vo.setRcode(residents.getRcode().substring(0,8));
		vo.setRname(residents.getRname());
		vo.setTel(residents.getTel());
		vo.setRid(room.getRcode());
		vo.setCtime(DateUtil.formatDateTime(new Date()));
		vo.setOpername(user.getName());
		vo.setPname(records.getPname());
		vo.setBfees(records.getBfees());
		vo.setWarrearage(records.getWarrearage());
		vo.setShouldpay(records.getShouldpay());
		vo.setRealpay(records.getRealpay());
		vo.setCharge(records.getCharge());
		vo.setMaxWrite(MoneyUtil.toChinese(vo.getShouldpay()));
		/*电费*/
		AnmeterRecords anmeterRecords=records.getAnmeterRecord();
		if(anmeterRecords!=null && (anmeterRecords.getReceivable()!=null 
				&& StringUtils.isNotEmpty(anmeterRecords.getReceivable())
				&& Float.parseFloat(anmeterRecords.getReceivable())>0)){
			List<AnmeterRecordItem> items=anmeterRecords.getItemsList();
			for(Iterator<AnmeterRecordItem> iter=items.iterator();iter.hasNext();){
				ItemVO item=new ItemVO();
				AnmeterRecordItem tmp=iter.next();
				item.setIname("电费");
				item.setCurrentreading(anmeterRecords.getCurrentreading());
				item.setThereadings(anmeterRecords.getThereadings());
				item.setWalk(anmeterRecords.getWalkquantity());
				item.setAmount(anmeterRecords.getAmount());
				item.setActual(anmeterRecords.getCharge());
				item.setTarget(tmp.getTarget());
				item.setCountsum(tmp.getWalk());
				item.setPrice(tmp.getPrice());
				item.setYmoney(tmp.getYmoney());
				vo.getItems().add(item);
			}
		}
		/*水费*/
		WatermeterRecords watermeterRecords=records.getWatermeterRecord();
		if(watermeterRecords!=null && (watermeterRecords.getReceivable()!=null 
				&& StringUtils.isNotEmpty(watermeterRecords.getReceivable())
				&& Float.parseFloat(watermeterRecords.getReceivable())>0)){
			List<WatermeterRecordItem> items=watermeterRecords.getItemsList();
			for(Iterator<WatermeterRecordItem> iter=items.iterator();iter.hasNext();){
				ItemVO item=new ItemVO();
				WatermeterRecordItem tmp=iter.next();
				item.setIname("水费");
				item.setCurrentreading(watermeterRecords.getCurrentreading());
				item.setThereadings(watermeterRecords.getThereadings());
				item.setWalk(watermeterRecords.getWalkquantity());
				item.setAmount(watermeterRecords.getAmount());
				item.setActual(watermeterRecords.getCharge());
				item.setTarget(tmp.getTarget());
				item.setCountsum(tmp.getWalk());
				item.setPrice(tmp.getPrice());
				item.setYmoney(tmp.getYmoney());
				vo.getItems().add(item);
			}
		}
		/*卫生费*/
		WsfRecord wsfRecord=records.getWsfRecord();
		if(wsfRecord!=null && (wsfRecord.getReceivable()!=null 
				&& StringUtils.isNotEmpty(wsfRecord.getReceivable())
				&& Float.parseFloat(wsfRecord.getReceivable())>0)){
			Set<WsfRecordItem> items=wsfRecord.getItems();
			for(Iterator<WsfRecordItem> iter=items.iterator();iter.hasNext();){
				ItemVO item=new ItemVO();
				WsfRecordItem tmp=iter.next();
				item.setIname(tmp.getFeesType().getFtname());
				item.setCountsum(tmp.getRnum());
				item.setPrice(tmp.getPrice());
				item.setYmoney(tmp.getYmoney());
				vo.getItems().add(item);
			}
		}
		/*护楼费*/
		HlfRecord hlfRecord=records.getHlfRecord();
		if(hlfRecord!=null && (hlfRecord.getCombined()!=null 
				&& StringUtils.isNotEmpty(hlfRecord.getCombined())
				&& Float.parseFloat(hlfRecord.getCombined())>0)){
			ItemVO item=new ItemVO();
			item.setIname("护楼费");
			item.setPrice(hlfRecord.getRealfees());
			item.setYmoney(hlfRecord.getRealfees());
			vo.getItems().add(item);
		}
		/*管理费*/
		GlfRecord glfRecord=records.getGlfRecord();
		if(glfRecord!=null && (glfRecord.getBsum()!=null 
				&& StringUtils.isNotEmpty(glfRecord.getBsum())
				&& Float.parseFloat(glfRecord.getBsum())>0)){
			ItemVO item=new ItemVO();
			item.setIname("管理费");
			item.setCountsum(glfRecord.getRnum());
			item.setPrice(glfRecord.getPrice());
			item.setYmoney(glfRecord.getLatefees());
			vo.getItems().add(item);
		}
		/*暖气费*/
		NqfRecord nqfRecord=records.getNqfRecord();
		if(nqfRecord!=null && (nqfRecord.getBsum()!=null 
				&& StringUtils.isNotEmpty(nqfRecord.getBsum())
				&& Float.parseFloat(nqfRecord.getBsum())>0)){
			ItemVO item=new ItemVO();
			item.setIname("暖气费");
			item.setYmoney(nqfRecord.getCombined());
			vo.getItems().add(item);
		}
		/*房费*/
		FfRecord ffRecord=records.getFfRecord();
		if(ffRecord!=null && (ffRecord.getCombined()!=null 
				&& StringUtils.isNotEmpty(ffRecord.getCombined())
				&& Float.parseFloat(ffRecord.getCombined())>0)){
			ItemVO item=new ItemVO();
			item.setIname("房费");
			if(ffRecord.getToll().equals("1")){
				item.setCountsum(ffRecord.getArea());
				item.setPrice(ffRecord.getPrice());
				item.setYmoney(ffRecord.getCombined());
			}else{
				item.setYmoney(ffRecord.getCombined());
			}
			vo.getItems().add(item);
		}
		/*其他费*/
		OtherRecord otherRecord=records.getOtherRecord();
		if(otherRecord!=null && (otherRecord.getCombined()!=null 
				&& StringUtils.isNotEmpty(otherRecord.getCombined())
				&& Float.parseFloat(otherRecord.getCombined())>0)){
			Set<OtherRecordItem> items=otherRecord.getItems();
			for(Iterator<OtherRecordItem> iter=items.iterator();iter.hasNext();){
				ItemVO item=new ItemVO();
				OtherRecordItem tmp=iter.next();
				item.setIname(tmp.getCostname());
				item.setYmoney(tmp.getCombined());
				vo.getItems().add(item);
			}
		}
		return vo;
	};
}
